//
//  MBSpaceNewsCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/20.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceNewsDetailCell.h"

@interface MBSpaceNewsDetailCell () {
    UIView                      *_bgView;
    UIImageView                 *_userImgView;
    UILabel                     *_nickNameLb;
    UILabel                     *_pinlun;
    UILabel                     *_timeLb;
    UILabel                     *_commentLb;
    UILabel                     *_replyLb;
    UIButton                    *_replyBt;
    UIView                      *_lineView;
}

@end
@implementation MBSpaceNewsDetailCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
        if (ISIPAD) {
            self.backgroundColor=[UIColorFromHex(0xffffff)colorWithAlphaComponent:.2];
        }
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _userImgView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_userImgView];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    [self.contentView addSubview:_nickNameLb];
    
    //评论
    _pinlun = [[UILabel alloc]initWithFrame:CGRectZero];
    _pinlun.backgroundColor = [UIColor clearColor];
    _pinlun.textAlignment = NSTextAlignmentLeft;
    _pinlun.textColor = UIColorFromHex(0x999999);
    _pinlun.font = [UIFont systemFontOfSize:12.f];
    _pinlun.text = @"评论";
    [self.contentView addSubview:_pinlun];
    
    //时间
    _timeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _timeLb.backgroundColor = [UIColor clearColor];
    _timeLb.textAlignment = NSTextAlignmentLeft;
    _timeLb.textColor = UIColorFromHex(0x999999);
    _timeLb.font = [UIFont systemFontOfSize:11.f];
    [self.contentView addSubview:_timeLb];
    
    //评论内容
    _commentLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _commentLb.backgroundColor = [UIColor clearColor];
    _commentLb.textAlignment = NSTextAlignmentLeft;
    _commentLb.textColor = UIColorFromHex(0x333333);
    _commentLb.font = [UIFont systemFontOfSize:13.f];
    _commentLb.numberOfLines = 0;
    [self.contentView addSubview:_commentLb];
    
    //回复
    _replyLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _replyLb.backgroundColor = [UIColor clearColor];
    _replyLb.textAlignment = NSTextAlignmentLeft;
    _replyLb.textColor = UIColorFromHex(0x333333);
    _replyLb.font = [UIFont systemFontOfSize:13.f];
    _replyLb.hidden = YES;
    [self.contentView addSubview:_replyLb];
    
    _replyBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [_replyBt setTitle:@"回复" forState:UIControlStateNormal];
    [_replyBt setTitle:@"回复" forState:UIControlStateHighlighted];
    [_replyBt setTitleColor:TINK_BG_COLOR forState:UIControlStateNormal];
    [_replyBt setTitleColor:TINK_BG_COLOR forState:UIControlStateHighlighted];
    _replyBt.titleLabel.font = _nickNameLb.font;
    [_replyBt setTitleEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    [_replyBt setImage:[UIImage imageNamed:@"huifu_pl"] forState:UIControlStateNormal];
    [_replyBt setImage:[UIImage imageNamed:@"huifu_pl"] forState:UIControlStateHighlighted];
    [_replyBt setImageEdgeInsets:UIEdgeInsetsMake(0, (WIDTH_5S ?20:10), 0, 0)];
    _replyBt.backgroundColor = [UIColor clearColor];
    
    
    [_replyBt addTarget:self action:@selector(onSelectReplyAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_replyBt];
    
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999)colorWithAlphaComponent:.15];
    [self.contentView addSubview:_lineView];
    
 }
-(void)setIsFromShow:(BOOL)isFromShow{
    _isFromShow=isFromShow;
    if (isFromShow) {
        if (ISIPAD) {
            _commentLb.textColor=UIColorFromHex(0xffffff);
            _commentLb.font=[UIFont systemFontOfSize:24*PERCEN];
            
            _pinlun.textColor=UIColorFromHex(0xffffff);
            _pinlun.font=[UIFont systemFontOfSize:28*PERCEN];
            
            _nickNameLb.textColor=UIColorFromHex(0xffffff);
            _nickNameLb.font=[UIFont systemFontOfSize:30*PERCEN];
            
            _timeLb.textColor=UIColorFromHex(0xffffff);
            _timeLb.font=[UIFont systemFontOfSize:24*PERCEN];
            
            _lineView.backgroundColor=UIColorFromHex(0xffffff);
            
            [_replyBt setTitleColor:UIColorFromHex(0xffffff) forState:0];
            _replyBt.imageEdgeInsets=UIEdgeInsetsMake(0, -5*PERCEN, 0, 0);
            _replyBt.titleEdgeInsets=UIEdgeInsetsMake(0, 10*PERCEN, 0, 0);
            
        }

    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

//回复
- (void)onSelectReplyAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectControlForReplyWithValue:withView:)]) {
        [_delegate onSelectControlForReplyWithValue:_dataDic withView:self];
    }
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    if (!_dataDic) {
        _dataDic = [[NSDictionary alloc]init];
    }
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    //value
    
    _replyLb.hidden = YES;
    
    
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.lineSpacing = 6*PERCEN;
    
    CGFloat contentHiehgt = 0;
    NSDictionary *ReplyInfo = [_dataDic objectForKey:@"ReplyInfo"];
    if ([ReplyInfo isKindOfClass:[NSDictionary class]]) {
        if (ReplyInfo.allKeys.count == 0) {
            //评论内容
            
            //头像
            NSString *faceImg = [_dataDic objectForKey:@"FaceImg"];
            if (![faceImg isKindOfClass:[NSNull class]]) {
                if (![faceImg hasPrefix:@"http"]) {
                    faceImg = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,faceImg];
                }
                
                [_userImgView sd_setImageWithURL:[NSURL URLWithString:faceImg] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
            } else {
                _userImgView.image = [UIImage imageNamed:@"story_header_bg"];
            }
            
            //昵称
            _nickNameLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"NickName"]];
            
            //时间
            _timeLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CreateTime"]] getChaTime];
            
            //内容
            NSString *content = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"Content"]] replaceUnicode];
            
            NSMutableAttributedString *mutAttributing = [[NSMutableAttributedString alloc]initWithString:content];
            [mutAttributing addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, content.length)];
            [mutAttributing addAttribute:NSFontAttributeName value:_commentLb.font range:NSMakeRange(0, content.length)];
            _commentLb.attributedText = mutAttributing;
            
            contentHiehgt = [mutAttributing boundingRectWithSize:CGSizeMake((ISIPAD?SCREEN_WIDTH-920*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):SCREEN_WIDTH-50*PERCEN), 1000) options:1|2 context:nil].size.height;
            if (_isFromShow) {
                if (ISIPAD) {
                    contentHiehgt = [mutAttributing boundingRectWithSize:CGSizeMake(700*PERCEN, 1000) options:1|2 context:nil].size.height;
                }
            }
          
            
        } else {
            //回复内容
            
            //头像
            NSString *faceImg = [ReplyInfo objectForKey:@"ReplyImg"];
            if (![faceImg isKindOfClass:[NSNull class]]) {
                if (![faceImg hasPrefix:@"http"]) {
                    faceImg = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,faceImg];
                }
                
                [_userImgView sd_setImageWithURL:[NSURL URLWithString:faceImg] placeholderImage:[UIImage imageNamed:@"story_header_bg"]];
            } else {
                _userImgView.image = [UIImage imageNamed:@"story_header_bg"];
            }
            
            //昵称
            _nickNameLb.text = [NSString stringWithFormat:@"%@",[ReplyInfo objectForKey:@"ReplyName"]];
            
            //时间
            _timeLb.text = [[NSString stringWithFormat:@"%@",[ReplyInfo objectForKey:@"CreateTime"]] getChaTime];
            
            //内容
            NSString *comment = [[NSString stringWithFormat:@"%@",[ReplyInfo objectForKey:@"ReplyCont"]] replaceUnicode];
            NSString *str1 = @"回复";
            NSString *str2 = [NSString stringWithFormat:@"%@",[ReplyInfo objectForKey:@"RepliedName"]];
            NSString *content = [NSString stringWithFormat:@"%@ %@: %@",str1,str2,comment];
            
            NSMutableAttributedString *mutAttributing = [[NSMutableAttributedString alloc]initWithString:content];
            [mutAttributing addAttribute:NSForegroundColorAttributeName value:TINK_BG_COLOR range:NSMakeRange(str1.length+1, str2.length)];
            [mutAttributing addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, content.length)];
            [mutAttributing addAttribute:NSFontAttributeName value:_commentLb.font range:NSMakeRange(0, content.length)];
            _commentLb.attributedText = mutAttributing;
            
            contentHiehgt = [mutAttributing boundingRectWithSize:CGSizeMake((ISIPAD?SCREEN_WIDTH-920*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):SCREEN_WIDTH-50*PERCEN), 1000) options:1|2 context:nil].size.height;
            if (_isFromShow) {
                
                if (ISIPAD) {
                    contentHiehgt = [mutAttributing boundingRectWithSize:CGSizeMake(700*PERCEN, 1000) options:1|2 context:nil].size.height;
                }
            }
           

        }
    }
    
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20*PERCEN+40+12*PERCEN+contentHiehgt+5);
    if (_isFromShow) {
        if (ISIPAD) {
            _bgView.frame = CGRectMake(0, 0, 858*PERCEN, 100*PERCEN+contentHiehgt+5);
            _bgView.backgroundColor=[UIColor clearColor];
            
        }
    }
    
    
    _userImgView.frame = CGRectMake((ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 20*PERCEN,ISIPAD?80*PERCEN:  65*PERCEN, ISIPAD?80*PERCEN:  65*PERCEN);
    if (_isFromShow) {
          _userImgView.frame = CGRectMake(30*PERCEN, 20*PERCEN,ISIPAD?80*PERCEN:  65*PERCEN, ISIPAD?80*PERCEN:  65*PERCEN);
    }
    _userImgView.layer.cornerRadius = _userImgView.height / 2.;
    _userImgView.layer.masksToBounds = YES;
    
    float nickWidth = [_nickNameLb.text boundingRectWithSize:CGSizeMake(1000, 20) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_nickNameLb.font} context:NULL].size.width+6;
    if (ISIPAD) {
        nickWidth = [_nickNameLb.text boundingRectWithSize:CGSizeMake(1000, 30*PERCEN) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:_nickNameLb.font} context:NULL].size.width+6;
    }
    _nickNameLb.frame = CGRectMake(_userImgView.right+20*PERCEN, _userImgView.top-(WIDTH_5S?2:0), nickWidth, ISIPAD?28*PERCEN:20);
    
    _pinlun.frame = CGRectMake(_nickNameLb.right+10*PERCEN, _nickNameLb.top, 80, ISIPAD?28*PERCEN:20);
    
    _timeLb.frame = CGRectMake(_nickNameLb.left, ISIPAD?_nickNameLb.bottom+20*PERCEN: _nickNameLb.bottom-(WIDTH_5S?4:2), 120,ISIPAD?24*PERCEN:20);
    
    _commentLb.frame = CGRectMake(_userImgView.left, _userImgView.bottom+12*PERCEN+5, (ISIPAD?_bgView.width-920*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):_bgView.width-50*PERCEN), contentHiehgt);
    if (_isFromShow) {
        if (ISIPAD) {
            _commentLb.frame = CGRectMake(_userImgView.left, _userImgView.bottom, 700*PERCEN, contentHiehgt);
        }
    }
  
//    _replyLb.frame = (_replyLb.hidden == YES ? CGRectZero : CGRectMake(_commentLb.left, _commentLb.bottom, _commentLb.width, 20));
    
    _replyBt.frame = CGRectMake(_bgView.width-80-(ISIPAD?460*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):20*PERCEN), _timeLb.top-10, 80, 40);
    if (_isFromShow) {
        if (ISIPAD) {
            _replyBt.frame=CGRectMake(708*PERCEN, _timeLb.top-20*PERCEN, 120*PERCEN, 30*PERCEN);
        }
    }
   
    _lineView.frame = CGRectMake(0, _bgView.height-1, _bgView.width, 1);
}

@end
